package com.example.onlineeducation.widget

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.DrawableRes
import com.example.onlineeducation.R
import com.yc.jetpacklib.extension.ycToNoEmpty

class LoginEditTextView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
    private var mEditText: EditText
    private var mTextView: TextView
    private var mImageView: ImageView


    private var textSize: Float = 0f

    private var editTextColor: Int = 0

    private var hintText: String = ""

    @DrawableRes
    private var icon: Int = 0

    private var focuseIconColor: Int = 0

    private var unfocusedIconColor: Int = 0

    @DrawableRes
    private var focusedBackground: Int = 0

    @DrawableRes
    private var unfocuseBackground: Int = 0

    private var isEditType: Boolean = true

    init {
        val typedArray: TypedArray =
            context.obtainStyledAttributes(attrs, R.styleable.LoginEditTextView)
        val text = typedArray.getString(R.styleable.LoginEditTextView_loginEditTextView_text)
            .ycToNoEmpty("")
        textSize =
            typedArray.getDimension(R.styleable.LoginEditTextView_loginEditTextView_textsize, 0f)
        hintText = typedArray.getString(R.styleable.LoginEditTextView_loginEditTextView_hinttext)
            .ycToNoEmpty("")
        icon = typedArray.getResourceId(R.styleable.LoginEditTextView_loginEditTextView_Icon, 0)
        editTextColor =
            typedArray.getColor(R.styleable.LoginEditTextView_loginEditTextView_textcolor, 0)
        focuseIconColor =
            typedArray.getColor(R.styleable.LoginEditTextView_loginEditTextView_focuseIconColor, 0)
        unfocusedIconColor = typedArray.getColor(
            R.styleable.LoginEditTextView_loginEditTextView_unfocusedIconColor,
            0
        )
        focusedBackground = typedArray.getResourceId(
            R.styleable.LoginEditTextView_loginEditTextView_focusedBackground,
            0
        )
        unfocuseBackground = typedArray.getResourceId(
            R.styleable.LoginEditTextView_loginEditTextView_unfocuseBackground,
            0
        )
        isEditType =
            typedArray.getBoolean(R.styleable.LoginEditTextView_loginEditTextView_isEditType, true)
        mImageView = ImageView(getContext())
        mImageView.setImageResource(icon)
        mImageView.scaleType = ImageView.ScaleType.FIT_XY
        mImageView.adjustViewBounds = true
        mImageView.setPadding(resources.getDimensionPixelOffset(R.dimen.activity_margin), 0, 0, 0)
        mImageView.layoutParams = LayoutParams(
            resources.getDimensionPixelOffset(R.dimen.activity_margin) + resources.getDimensionPixelOffset(
                R.dimen.login_edittext_width
            ),
            resources.getDimensionPixelOffset(R.dimen.login_edittext_width)
        )
        addView(mImageView)
        mEditText = EditText(getContext())
        mEditText.background = null
        mEditText.layoutParams = LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT
        )
        mEditText.setText(text)
        mEditText.paint.textSize = textSize
        mEditText.setTextColor(editTextColor)
        mEditText.hint = hintText
        mEditText.isSingleLine = true
        addView(mEditText)
        gravity = Gravity.CENTER_VERTICAL
        mImageView.setColorFilter(unfocusedIconColor)
        setBackgroundResource(R.drawable.shape_ring_gray)
        mEditText.setOnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                mImageView.setColorFilter(focuseIconColor)
                setBackgroundResource(focusedBackground)
            } else {
                mImageView.setColorFilter(unfocusedIconColor)
                setBackgroundResource(unfocuseBackground)
            }
        }

        mTextView = TextView(getContext())
        mTextView.background = null
        mTextView.layoutParams = LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT
        )
        mTextView.setText(text)
        mTextView.paint.textSize = textSize
        mTextView.setTextColor(editTextColor)
        mTextView.hint = hintText
        mTextView.isSingleLine = true
        addView(mTextView)

        if (isEditType) {
            mEditText.visibility = View.VISIBLE
            mTextView.visibility = View.GONE
        } else {
            mEditText.visibility = View.GONE
            mTextView.visibility = View.VISIBLE
        }
        // typedArray.recycle()
    }

    fun setText(str: String) {
        if (isEditType) {
            mEditText.setText(str)
        } else {
            mTextView.setText(str)
        }

    }

    fun getText(): String {
        return if (isEditType) {
            mEditText.text.toString()
        } else {
            mTextView.text.toString()
        }
    }

    fun getEdt(): EditText {
        return mEditText
    }

    fun setParentClick(onClick: OnClickListener) {
        /*     mEditText.isEnabled = enable
             mEditText.isFocusable = enable
             mEditText.isClickable = enable
             if (enable) {
                 mEditText.isFocusableInTouchMode = enable
             }*/
        mTextView.setOnClickListener(onClick)
        setOnClickListener(onClick)
    }

}